package com.fare.taluo.core.domain.freightprice;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.log4j.Logger;

import com.fare.taluo.core.domain.freightprice.em.PriceCabin;
import com.fare.taluo.core.domain.freightprice.em.PriceKind;
/**
 * 航班运营核心模型
 * @author creator: xiangsf
 * @author modifior: xiangsf
 * @since 2012-12-25-上午9:55:01
 * @version v1.0
 */
public class FreightFlightEntity extends AbstractFreightFlightEntity{
	
	 protected Logger logger = Logger.getLogger(getClass());
	
	 public static final String HTABLE_FREIGHT_FLIGHT_DAY = "FLIGHT_LOWEST_PRICE";
	 /**
	  * 主键
	  */
	 protected String rowkey;
	 /**
	  * 航空公司代码
	  */
	 protected String carrierCode;
	 /**
	  * 航空公司名称
	  */
	 protected String carrierName;
	 /**
	  * 航空公司简称
	  */
	 protected String carrierSimpleName;
	 /**
	  * 航段
	  */
	 protected FlightLegEntity flightLeg;
	 /**
	  * 价格
	  */
	 protected Set<PriceEntity> prices = new HashSet<PriceEntity>();
	 /**
	  * 最后更新时间
	  */
	 protected Date updateDate;

	 
	public void setRowkey(String rowkey) {
		this.rowkey = rowkey;
	}

	public String getRowkey() {
		return rowkey;
	}

	public String getCarrierCode() {
		return carrierCode;
	}

	public String getFromCity() {
		return flightLeg.getFromCity();
	}

	public String getToCity() {
		return flightLeg.getToCity();
	}

	public void addFlightLeg(int priority, String fromCity, String toCity){
		this.flightLeg = new FlightLegEntity(priority, fromCity, toCity);
	}
	
	public double getDefaultPrice() {
		PriceEntity price = this.prices.iterator().next();
		return price != null ? price.getPrice() : -1;
	}
	public Set<PriceEntity> getPrices() {
		return this.prices;
	}
	public void addPrice(double price) {
		this.prices.add(new PriceEntity(PriceCabin.UNKOWN,PriceKind.UNKOWN,price));
	}
	
	public void addPrice(PriceEntity price) {
		this.prices.add(price);
	}
	
	public Date getUpdateDate() {
		return updateDate;
	}

	public String getCarrierName() {
		return carrierName;
	}

	public String getCarrierSimpleName() {
		return carrierSimpleName;
	}
	/**
	 * 根据hbase查询结果行，创建对象实体
	 * agentName 代理公司名称
		carrierFullName 航空公司全名
		carrierKey 航空公司代码
		carrierName 航空公司简名
		flightBatch 航班号
		flightBatchDate 航班日期
		flightBatchEndTime 到达时间
		flightBatchStartTime 起飞时间
		flightId 航班号
		fromCity 出发城市
		grabChannel 抓取渠道
		highPrice 最高价
		lowprPrice 最低价
		toCity 目的城市
		type 类型
		createDate 预处理时间
	family列簇 planeinfo
	rowkey: 
	 */
	@Override
	public FreightFlightEntity createEntity(KeyValue kv) throws Exception {
		String fromCity = null;
		String toCity = null;
		String	family = new String(kv.getFamily(), "utf-8");
		String	qualifier = new String(kv.getQualifier(), "utf-8");
		String	value = new String(kv.getValue(), "utf-8");
		//获得，航空公司代码，航空公司名称，航空公司全称，出发地，目的地，最低价
				if("planeinfo".equalsIgnoreCase(family)){
					if("carrierKey".equalsIgnoreCase(qualifier)){
						this.carrierCode = value;
					}else if("carrierName".equalsIgnoreCase(qualifier)){
						this.carrierSimpleName = value;
					}else if("carrierFullName".equalsIgnoreCase(qualifier)){
						this.carrierName = value;
					}else if("fromCity".equalsIgnoreCase(qualifier)){
						fromCity = value;
					}else if("toCity".equalsIgnoreCase(qualifier)){
						toCity = value;
					}else if("lowprPrice".equalsIgnoreCase(qualifier)){
						this.addPrice(Double.valueOf(qualifier));
					}else if("createDate".equalsIgnoreCase(qualifier)){
						this.updateDate = DateUtils.parseDate(value, "yyyy-MM-dd HH:mm:ss");
					}
				}
		this.addFlightLeg(1, fromCity, toCity);
		return this;
	}
	
	
}
